/*Lock and CV test with multithreads*/

#include "syscall.h"
#include "user.h"

#define MAXLOCKNUM 1024

int lock_t1;
int lock_t2;
int cv_t2;



void t1_thread1(void) {
	Printf("Excepted: Fatal DestroyLock Error: The lock you are trying to destroy is in USE!\n");
	DestroyLock(lock_t1);
	Exit(0);	
}

void t2_thread1(void) {
	Printf("thread1: I entered\n");
	AcquireLock(lock_t2);
	Printf("thread1:I prepare to wait!\n");
	WaitCV(lock_t2,cv_t2);
	Printf("thread1:I was waked up!\n");
	ReleaseLock(lock_t2);
	Exit(0);
}
void t2_thread2(void) {
	Printf("thread2: I entered\n");
	AcquireLock(lock_t2);
	Printf("thread2: I gonna wake thread1\n");
	SignalCV(lock_t2,cv_t2);
	ReleaseLock(lock_t2);
	Exit(0);
}

void initialLock(void) {
	lock_t1 = CreateLock("lock_t1 ",sizeof("lock_t1"));
	lock_t2 = CreateLock("lock_t2 ",sizeof("lock_t2"));
}

void initialCV(void) {
	cv_t2 = CreateCondition("cv_t2",sizeof("cv_t2"));
}


void main(void) {

	initialLock();
	initialCV();

	Printf("\ntest1 : Destroylock test\n"); 
	AcquireLock(lock_t1);
	Fork(t1_thread1);
	Printf("\ntest2 : Wait and Signal Lock between different threads\n"); 
	Fork(t2_thread1);
	Fork(t2_thread2);
	Exit(0);
	
}

